package com.jirong.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jirong.shop.eException.GlobalException;
import com.jirong.shop.mapper.TUserMapper;
import com.jirong.shop.pojo.TUser;
import com.jirong.shop.service.TUserService;
import com.jirong.shop.utils.CookieUtil;
import com.jirong.shop.utils.MD5Util;
import com.jirong.shop.utils.UUIDUtil;
import com.jirong.shop.vo.LoginVo;
import com.jirong.shop.vo.RespBean;
import com.jirong.shop.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jirong
 * @since 2022-01-13
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Autowired
    private TUserMapper userMapper;
    @Autowired
    //    改下用的名字不然一直导错类
    @Qualifier("redisTemplate1")
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        用工具类可以避免冗余判断
//        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)) {
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
//        从数据库查账号
        TUser tUser = userMapper.selectById(mobile);
        if (tUser == null) {
//            return  RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
//        比较加密密码
        if (!MD5Util.formPassToDBPass(password, tUser.getSlat()).equals(tUser.getPassword())) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
//        法一cookie比较
//        生成cookie
        String ticket = UUIDUtil.uuid();
//        将用户存入session中
//        request.getSession().setAttribute(ticket,tUser);
//        存在redis中
           redisTemplate.opsForValue().set("user:"+ticket,tUser);
//        使用工具类生成cookie
        CookieUtil.setCookie(request,response,"ticketUser",ticket);
//        String ticketUser = CookieUtil.getCookieValue(request, "ticketUser");
//        System.out.println(ticketUser);
//        法二要拿到每次登陆的cookie,必须返回ticket
        return RespBean.success(ticket);
        //        法二jwt生成token验证
    }

    @Override
    public TUser getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)) {
            return null;
        }
//        获得redis中的user用户
        TUser user = (TUser) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user != null) {
//            再次设置cookie
            CookieUtil.setCookie(request, response, "ticketUser", userTicket);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, Long id, String password) {
        TUser user = userMapper.selectById(id);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
//        进行更新操作
        int i = userMapper.updateById(user);
        if(i==1){
//            替换原来的密码或者删除原密码在储存
//            替换方法要获得key和新的对象
//            删除缓存下一次登录就会有新的缓存进去，确保你从缓存中拿的东西是最新的
//            redisTemplate.opsForValue().getAndSet(user)
            //删除Redis
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();

        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
